﻿/**********************************************************************
*创建人：蔡水明
*创建时间：2017/12/22 16:26:47
*说明：<Function>
/**********************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities
{
    public class LogHelper
    {
        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <param name="txt"></param>
        private static void writeLog(string txt)
        {
            //string logFile = Path.Combine("C:\\Log", "Log" + DateTime.Now.ToString("yyyy - MM - dd") + ".txt");
            //string logPath = "C:\\Log";

            string path = AppDomain.CurrentDomain.BaseDirectory;
            string logFile = Path.Combine(path, @"Log\Log" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
            string logPath = Path.Combine(path, "Log");
            txt = $"-----{DateTime.Now.ToString("HH:mm:ss")}-----\r\n{txt}\r\n--------------------";
            if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
            StreamWriter sw = File.AppendText(logFile);
            sw.WriteLine(txt);
            sw.Close();
        }
        private static string Export2String(Exception e)
        {
            string str = string.Empty;
            str = $"错误详情\r\n==============\r\n错误消息：{e.Message}\r\n";
            str += $"=====\r\n跟踪信息：{e.StackTrace}\r\n=====";
            str += $"异常源头：{e.Source}\r\n异常数据：=====\r\n";
            foreach (var item in e.Data.Values)
            {
                str += $"异常数据Value：{item.ToString()}\r\n";
            }
            return str;

        }
        public static bool IsDebugEnabled
        {
            get
            {
                return false;
            }
        }

        public static void Warn(string text)
        {
            writeLog($"Warn:\r\n{text}");
        }

        public static void WarnFormat(string format, params object[] args)
        {
            writeLog($"WarnFormat:\r\n{format}");
        }

        public static void Warn(Exception e)
        {
            writeLog($"Warn:\r\n{Export2String(e)}");
        }

        public static void Warn(string text, Exception e)
        {
            writeLog($"Warn:\r\ntext:{text}\r\n{Export2String(e)}");
        }

        public static void Error(string text)
        {
            writeLog($"Error:\r\n{text}");
        }

        public static void ErrorFormat(string format, params object[] args)
        {
        }

        public static void Error(Exception e)
        {
            writeLog($"Error:\r\n{Export2String(e)}");
        }

        public static void Error(string text, Exception e)
        {
            writeLog($"Error:\r\n{text}\r\n{Export2String(e)}");
        }

        public static void Info(string text)
        {
            writeLog($"Info:\r\n{text}");
        }

        public static void InfoFormat(string format, params object[] args)
        {
            writeLog($"format:\r\n{format}");
        }

        public static void Debug(string text)
        {
            writeLog($"Debug:\r\n{text}");
        }

        public static void DebugFormat(string text, params object[] args)
        {
            writeLog($"DebugFormat:\r\n{text}");
        }

        public static void Debug(string text, Exception e)
        {
            writeLog($"Debug:\r\n{text}\r\n{Export2String(e)}");
        }
        /// <summary>
        /// 写入报告
        /// </summary>
        /// <param name="txt"></param>
        public static void WriteReport(string txt)
        {
            string logFile = System.IO.Path.Combine(Environment.CurrentDirectory, @"Cfg\Report.txt");
            string logPath = System.IO.Path.Combine(Environment.CurrentDirectory, "Cfg");
            txt = $"-----{DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss")}-----\r\n{txt}\r\n--------------------";
            if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
            StreamWriter sw = File.AppendText(logFile);
            sw.WriteLine(txt);
            sw.Close();
        }
        /// <summary>
        /// 把文本文件内容读出来
        /// </summary>
        /// <param name="filename">文件名称</param>        
        public static string FileToString(string filename)
        {
            if (File.Exists(filename) == false)
            {                
                return string.Empty;
            }
            StreamReader sr = new StreamReader(filename, Encoding.UTF8);
            string line = sr.ReadToEnd();
            sr.Close();
            return line;
        }
    }
}
